<template>
  <div class="home-container">
    <div class="header">
      <h1>欢迎来到主页</h1>
      <button @click="handleLogout" class="logout-btn">退出登录</button>
    </div>
    <div class="content">
      <p>您已成功登录系统！</p>
      <div class="user-info">
        <h3>用户信息</h3>
        <p>用户名: {{ loginName }}</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const loginName = ref('');

// 获取用户信息
onMounted(() => {
  // 从localStorage中获取用户信息
  const userInfo = localStorage.getItem('userInfo');
  if (userInfo) {
    const parsedUser = JSON.parse(userInfo);
    loginName.value = parsedUser.login_name || '未知用户';
  } else {
    // 如果没有用户信息，跳转回登录页
    router.push('/');
  }
});

// 退出登录
const handleLogout = () => {
  // 清除用户信息
  localStorage.removeItem('userInfo');
  // 跳转到登录页
  router.push('/');
};
</script>

<style scoped>
.home-container {
  max-width: 800px;
  margin: 2rem auto;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #eee;
  padding-bottom: 1rem;
}

.logout-btn {
  background-color: #ff4d4f;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

.logout-btn:hover {
  background-color: #d73435;
}

.content {
  margin-top: 2rem;
}

.user-info {
  background-color: #f5f5f5;
  padding: 1rem;
  border-radius: 4px;
  margin-top: 1rem;
}
</style>